#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        vector<int> ret;
        int n = nums.size();
        if (n == 0)
        {
            ret.push_back(-1);
            ret.push_back(-1);
            return ret;
        }
        int left = 0;
        int right = n - 1;
        while (left < right)
        {
            int mid = (left + right) / 2;
            if (nums[mid] >= target)
            {
                right = mid;
            }
            else
            {
                left = mid + 1;
            }
        }
        if (nums[left] != target)
        {
            ret.push_back(-1);
            ret.push_back(-1);
            return ret;
        }
        else
        {
            ret.push_back(left);
        }
        right = n - 1;
        while (left < right)
        {
            int mid = (left + right + 1) / 2;
            if (nums[mid] > target)
            {
                right = mid - 1;
            }
            else
            {
                left = mid;
            }
        }
        ret.push_back(left);
        return ret;
    }
};

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        vector<int> ret;
        int n = nums.size();
        if (n == 0)
        {
            ret.push_back(-1);
            ret.push_back(-1);
            return ret;
        }
        int left = 0;
        int right = n - 1;
        while (left < right)
        {
            int mid = (left + right) / 2;
            if (nums[mid] < target)
            {
                left = mid + 1;
            }
            else
            {
                right = mid;
            }
        }
        if (nums[left] != target)
        {
            ret.push_back(-1);
            ret.push_back(-1);
            return ret;
        }
        else
        {
            ret.push_back(left);
        }
        right = n - 1;
        while (left < right)
        {
            int mid = (left + right) / 2 + 1;
            if (nums[mid] <= target)
            {
                left = mid;
            }
            else
            {
                right = mid - 1;
            }
        }
        ret.push_back(right);
        return ret;
    }
};

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int n = nums.size();
        int left = 0;
        int right = n - 1;
        while (left < right)
        {
            int mid = (left + right) / 2;
            if (nums[mid] < target)
            {
                left = mid + 1;
            }
            else
            {
                right = mid;
            }
        }
        if (left == n - 1 && nums[left] < target)
        {
            return left + 1;
        }
        return left;
    }
};

class Solution {
public:
    int peakIndexInMountainArray(vector<int>& arr) {
        int left = 0;
        int right = arr.size() - 2;
        while (left < right)
        {
            int mid = (left + right + 1) / 2;
            if (arr[mid - 1] < arr[mid])
            {
                left = mid;
            }
            else
            {
                right = mid - 1;
            }
        }
        return left;
    }
};

class Solution {
public:
    int findPeakElement(vector<int>& nums) {
        int left = 0;
        int right = nums.size() - 1;
        while (left < right)
        {
            int mid = (left + right + 1) / 2;
            if (nums[mid] > nums[mid - 1])
            {
                left = mid;
            }
            else
            {
                right = mid - 1;
            }
        }
        return left;
    }
};

class Solution {
public:
    int findPeakElement(vector<int>& nums) {
        int left = 0;
        int right = nums.size() - 1;
        while (left < right)
        {
            int mid = (left + right) / 2;
            if (nums[mid] > nums[mid + 1])
            {
                right = mid;
            }
            else
            {
                left = mid + 1;
            }
        }
        return left;
    }
};

class Solution {
public:
    vector<int> findPeaks(vector<int>& mountain) {
        int n = mountain.size();
        vector<int> ret;
        int left = 1;
        while (left < n - 1)
        {
            if (mountain[left] > mountain[left - 1] && mountain[left] > mountain[left + 1])
            {
                ret.push_back(left);
            }
            left++;
        }
        return ret;
    }
};